#!/system/bin/sh
# Project WIPE https://github.com/yc9559/cpufreq-interactive-opt
# Author: yc9559
# Generated at: Thu Apr 26 04:26:47 2018

# $1:value $2:file path
function set_value() {
	if [ -f $2 ]; then
		chown 0.0 $2
		chmod 0666 $2
		echo $1 > $2
		chmod 0444 $2
	fi
}

# $1:cpu0 $2:timer_rate $3:value
function set_param() {
	echo $3 > /sys/devices/system/cpu/$1/cpufreq/interactive/$2
}

action=$1
setenforce 0

# disable hotplug to switch governor
echo 0 > /sys/module/msm_thermal/core_control/enabled

set_value 1 /sys/devices/system/cpu/cpu4/online
chown 0.0 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chown 0.0 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*

# RunOnce
if [ ! -f /project_wipe_runonce ]; then
	# set flag
	touch /project_wipe_runonce

	# Perfd, nothing to worry about, if error the script will continue
	stop perfd

	set_value 90 /proc/sys/kernel/sched_spill_load
	set_value 0 /proc/sys/kernel/sched_boost
	set_value 1 /proc/sys/kernel/sched_prefer_sync_wakee_to_waker
	set_value 40 /proc/sys/kernel/sched_init_task_load
	set_value 3000000 /proc/sys/kernel/sched_freq_inc_notify

	set_value 50 /sys/module/cpu_boost/parameters/input_boost_ms
	set_value 0 /sys/module/msm_performance/parameters/touchboost

	set_value 380000 /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
	set_value 380000 /sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq

	# shared interactive parameters
	set_param cpu0 timer_rate 20000
	set_param cpu4 timer_rate 20000
	set_param cpu0 timer_slack 180000
	set_param cpu4 timer_slack 180000
	set_param cpu0 io_is_busy 0
	set_param cpu4 io_is_busy 0
	set_param cpu0 use_sched_load 1
	set_param cpu4 use_sched_load 1
	set_param cpu0 ignore_hispeed_on_notif 1
	set_param cpu4 ignore_hispeed_on_notif 1
	set_value 0 /sys/devices/system/cpu/cpu0/cpufreq/interactive/enable_prediction
	set_value 0 /sys/devices/system/cpu/cpu4/cpufreq/interactive/enable_prediction
fi

if [ "$action" = "powersave" ]; then
	set_value "0:1180000 4:0" /sys/module/cpu_boost/parameters/input_boost_freq
	set_param cpu0 above_hispeed_delay "38000 1280000:58000 1480000:18000"
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 go_hispeed_load 98
	set_param cpu0 boostpulse_duration 18000
	set_param cpu0 target_loads "85 580000:47 680000:58 780000:54 880000:85 1180000:74 1280000:98 1480000:94"
	set_param cpu0 min_sample_time 18000
	set_param cpu4 above_hispeed_delay "18000 1280000:58000 1680000:38000"
	set_param cpu4 hispeed_freq 1180000
	set_param cpu4 go_hispeed_load 97
	set_param cpu4 boostpulse_duration 18000
	set_param cpu4 target_loads "85 480000:46 580000:68 680000:48 780000:72 880000:99 1180000:77 1280000:89 1380000:99"
	set_param cpu4 min_sample_time 18000
fi

if [ "$action" = "balance" ]; then
	set_value "0:1180000 4:0" /sys/module/cpu_boost/parameters/input_boost_freq
	set_param cpu0 above_hispeed_delay "38000 1280000:58000"
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 go_hispeed_load 98
	set_param cpu0 boostpulse_duration 18000
	set_param cpu0 target_loads "85 580000:39 780000:54 880000:69 1280000:98 1480000:94"
	set_param cpu0 min_sample_time 18000
	set_param cpu4 above_hispeed_delay "18000 1280000:58000 1680000:18000"
	set_param cpu4 hispeed_freq 1180000
	set_param cpu4 go_hispeed_load 99
	set_param cpu4 boostpulse_duration 18000
	set_param cpu4 target_loads "85 480000:50 580000:67 680000:49 780000:71 880000:75 1280000:91 1380000:99"
	set_param cpu4 min_sample_time 18000
fi

if [ "$action" = "performance" ]; then
	set_value "0:1180000 4:0" /sys/module/cpu_boost/parameters/input_boost_freq
	set_param cpu0 above_hispeed_delay "38000 1280000:58000 1480000:18000"
	set_param cpu0 hispeed_freq 1180000
	set_param cpu0 go_hispeed_load 98
	set_param cpu0 boostpulse_duration 18000
	set_param cpu0 target_loads "85 580000:7 680000:36 780000:45 880000:54 1180000:69"
	set_param cpu0 min_sample_time 18000
	set_param cpu4 above_hispeed_delay "18000 1280000:58000 1680000:18000"
	set_param cpu4 hispeed_freq 1180000
	set_param cpu4 go_hispeed_load 97
	set_param cpu4 boostpulse_duration 18000
	set_param cpu4 target_loads "85 480000:29 580000:76 680000:49 780000:31 880000:54 1280000:70 1380000:99"
	set_param cpu4 min_sample_time 18000
fi

if [ "$action" = "fast" ]; then
	# nothing
fi

chmod 0444 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chmod 0444 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*

# re-enable hotplug to switch governor
echo 1 > /sys/module/msm_thermal/core_control/enabled

exit 0